package nl.fonfolio.view {
	//import fl.core.UIComponent;	
	
	
	import nl.fonfolio.core.FonfolioEvent;	
	import nl.fonfolio.controller.Controller;	
	import nl.fonfolio.model.Model;	
	
	import flash.display.*;
	
	import caurina.transitions.Tweener;
	
	import flash.events.MouseEvent;
	import flash.events.Event;


	/**
	 * @author Paul
	 */
	public class Fullscreen extends Sprite {

		public var fonfolio : Sprite;
		public var background : MovieClip;
	
		public var widgetcontainerLeft : Sprite = new Sprite();
		public var widgetcontainerRight : Sprite = new Sprite();
		public var close_btn : MovieClip;	
		
		private var model : Model;
		private var controller : Controller;
		
		public var opened:Boolean; //false=closed,true=opened
		
		public function Fullscreen(fonfolio:Sprite, model:Model, controller:Controller) 
		{
			this.controller = controller;
			this.model = model;
			this.fonfolio = fonfolio;
			this.widgetcontainerLeft = new Sprite();
			this.widgetcontainerRight = new Sprite();
			
			this.addChild(this.widgetcontainerLeft);
			this.addChild(this.widgetcontainerRight);

			//size vlak
			this.fonfolio.stage.addEventListener(Event.RESIZE, this.onResizeStage);
			//this.model.addEventListener(FonfolioEvent.CONTROL_TYPE, this.openFullscreenHandler);
			this.close_btn.addEventListener(MouseEvent.CLICK, this.close);
		
			this.resize();
			this.x = this.fonfolio.stage.stageWidth;
			this.background.width = this.fonfolio.stage.stageWidth;
			this.close_btn.x = this.fonfolio.stage.stageWidth - 50;
		}
		
		
		private function onResizeStage(event : Event) : void {
			this.resize();
		}
		
		private function resize() : void {   
			this.background.x = 0;
			this.background.height = this.fonfolio.stage.stageHeight;
			this.widgetcontainerLeft.y = this.height / 2 - this.widgetcontainerLeft.height / 2 ;
			this.widgetcontainerRight.y = this.height / 2 - this.widgetcontainerRight.height / 2 ;
			if (this.opened == true){
				this.x = 0;
			}
			else {
				this.x = this.fonfolio.stage.stageWidth;
			}
			this.background.width = this.fonfolio.stage.stageWidth;
			this.widgetcontainerLeft.x = (this.background.width/2)-this.widgetcontainerLeft.width;
			this.widgetcontainerRight.x =  this.background.width/2;
			this.close_btn.x = this.background.width - 40;
		}

		public function removeFullscreen():void{
			this.parent.removeChild(this);
		}


		public function close(event : Event=null) : void {
			this.opened = false;
			Tweener.addTween(this, {x:this.fonfolio.stage.stageWidth, time:0.5, transition:"easeInQuart", onComplete:removeFullscreen});
		}
		
		/*public function openFullscreenHandler(event : FonfolioEvent) : void {
			//trace("i've heard 'em shout: "+ event.command);
			if (event.command == "OPEN_FULLSCREEN"){
				trace("SO I OPEN FULLSCREEN PANEL");
				Tweener.addTween(this, {x:this.fonfolio.stage.stageWidth-this.background.width, time:0.5, transition:"easeOutQuart", onComplete:fullscreenLoaded});
			}
		}*/
		
		public function fullscreenLoaded():void{
			this.opened = true;
			//this.widgetcontainerLeft = new Sprite();
			//this.widgetcontainerRight = new Sprite();
		}
		
		public function reopen(event : Event) : void {
			//Tweener.addTween(this, {x:this.fonfolio.stage.stageWidth, time:0.8, transition:"easeInQuart",onComplete:open});
		} 
		
		public function open():void{
			Tweener.addTween(this, {x:this.fonfolio.stage.stageWidth-this.background.width, time:0.5, transition:"easeOutQuart", onComplete:fullscreenLoaded});
		}
		
		public function addWidgetLeft(widget : Sprite) : void {
			widget.y = this.widgetcontainerLeft.height;
		//	widget.x = (this.background.width/2)-widget.width;
			this.widgetcontainerLeft.addChild(widget);
			this.resize();
		}
		
		public function addWidgetRight(widget : Sprite) : void {
			widget.y = this.widgetcontainerRight.height;
			//widget.x = this.background.width/2;
			this.widgetcontainerRight.addChild(widget);
			this.resize();
		}
		
	}
}
